# -*- coding:utf-8 -*-
import requests
import re
from bs4 import BeautifulSoup
#添加请求头
HEADERS = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
def getYoudaoWebByWord(word):
    resp = requests.get('https://dict.youdao.com/result?word=%s&lang=en' % word,headers = HEADERS)
    return resp.text
def getPattern(exp):
    return re.compile(exp)
def getTargetList(exp,content):
    p = re.compile(exp)
    return p.findall(content)

def getWordMean(word):
    html = getYoudaoWebByWord(word).replace('\n','').replace('\t','').replace('\r','')
    #--------bs4 process begin--------#
    #with open('C:/E/temp/pic/neglect.html','r',encoding='utf-8') as htmlfile:
    #    html = htmlfile.read()
    bsp = BeautifulSoup(html,'html.parser')
    phonetic_list = bsp.select('.phonetic')
    word_dict_list = bsp.select('ul.basic')
    word_dict_list = str(word_dict_list).replace('<p','<span').replace('</p>','</span>')
    web_phrase_list = bsp.select('div.webPhrase > ul > li.mcols-layout')
    web_phrase_list = str(web_phrase_list).replace('<p','<span').replace('</p>','</span>').replace(',','')
    phrs_list = bsp.select('div.phrs.dict-module > ul.trans-container > li')
    phrs_list = str(phrs_list).replace(',','')
    #--------bs4 process end----------#
    text = '%s\n%s\n%s\n%s\n' % (phonetic_list,word_dict_list,web_phrase_list,phrs_list)
    return text

def getYoudaoWebBySentenceMean(sentence):
    '''https://dict.youdao.com/result?word=%s&lang=en'''
    resp = requests.get('https://dict.youdao.com/result?word=%s&lang=en' % sentence,headers = HEADERS)
    return resp.text

def getSentenceMean(sentence):
    html = getYoudaoWebBySentenceMean(sentence).replace('\n','').replace('\t','').replace('\r','')
    #with open('C:/E/temp/pic/sentence.html','r',encoding='utf-8') as sentencefile:
    #    html = sentencefile.read()
    bsp = BeautifulSoup(html,'html.parser')
    texts = bsp.select('.trans-content')
    sentence = ''
    for _t in texts:
        sentence = sentence + _t.get_text()
    return sentence

if __name__ == '__main__':
    try:
        print(getSentenceMean('the Chinese nation will never succumb to blackmail or sheer pressure.'))
    except Exception as ex:
        print('__main__::: ' + str(ex))
    finally:
        pass